package Class;

/**
 * @author 王艺锡
 * @version 1.0
 */
//获取class类对象的方式
public class GetClass {
    public static void main(String[] args) throws Exception {
        //1.class.forName 多用于配置文件，读取类全路径，加载类
        String classPath = "Class.Car";
        Class<?> cls1 = Class.forName(classPath);
        System.out.println(cls1);

        //2.类名.class  多用于参数传递，比如通过反射得到对应的构造器对象
        Class cls2 = Car.class;
        System.out.println(cls2);

        //3.对象.getClass
        Car car = new Car();
        Class cls3 = car.getClass();
        System.out.println(cls3);

        //4.通过类加载器来获取到类的Class对象
        //(1)先获取到类加载器
        ClassLoader classLoader = car.getClass().getClassLoader();
        //(2)通过类加载器获取到Class对象
        Class cls4 = classLoader.loadClass(classPath);
        System.out.println(cls4);

        //一个类，只有一个Class类,所以cls1，cls2，cls3，cls4都是同一个对象
        System.out.println(cls1.hashCode());
        System.out.println(cls2.hashCode());
        System.out.println(cls3.hashCode());
        System.out.println(cls4.hashCode());

        //5.基本数据类型，通过.class获取Class类
        Class<Integer> integerClass = int.class;
        Class<Character> characterClass = char.class;
        Class<Boolean> booleanClass = boolean.class;
        System.out.println(integerClass);
        System.out.println(characterClass);
        System.out.println(booleanClass);

        //6.基本数据类型对应的包装类，可以通过TYPE来得到Class类对象
        Class<Integer> type = Integer.TYPE;
        Class<Character> type1 = Character.TYPE;
        Class<Boolean> type2 = Boolean.TYPE;
        System.out.println(type);
        System.out.println(type1);
        System.out.println(type2);


        System.out.println(integerClass.hashCode());
        System.out.println(type.hashCode());

    }
}
